package com.fjt.book_trade.service;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.fjt.book_trade.dao.IUserDao;
import com.fjt.book_trade.pojo.User;

@Service("userService")
public class UserService {
//	public ApplicationContext context=new ClassPathXmlApplicationContext("spring-mybatis.xml");
//	private IUserDao userDao=context.getBean("IUserDao",IUserDao.class);
	private IUserDao userDao;
	private User user;

	public User getUser() {
		return user;
	}
	@Resource(name="user")
	public void setUser(User user) {
		this.user = user;
	}
	public IUserDao getUserDao() {
		return userDao;
	}
	
	@Resource
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}
	
	public Map<String, Object> getUserInfo(String username){
		Map<String, Object> map=new HashMap<String, Object>();
		User user=userDao.findUserInfoByName(username);
		if(user!=null){
			map.put("result", "success");
			map.put("userInfo", user);
		}else{
			map.put("result", "fail");
		}
		return map;
	}
	
	public boolean validate(User user){//验证用户
		System.out.println(user.getUsername());
    	System.out.println(user.getPassword());
 
		if(userDao.validateUser(user)!=null){
			System.out.println("有");
			return true;
		}else{
			System.out.println("空");
			return false;
		}
		
	}
	public Map<String,String> validateUser(User user,HttpServletRequest request){//验证用户，返回结果
		Map<String,String> map = new HashMap<String,String>();
		System.out.println(user.getUsername());
    	System.out.println(user.getPassword());
		if(validate(user)){
			System.out.println("登录成功！");
			map.put("result", "登录成功");
			HttpSession session=request.getSession();
			session.setAttribute("username", user.getUsername());
			System.out.println(session.getId());
		}else{
			System.out.println("登录失败！");
			map.put("result", "登录失败");
		}
		return map;
	}
	
	public Map<String,String> addUser(User user){//添加用户
		Map<String,String> map = new HashMap<String,String>();
//		int id = (int)(Math.random()*50000)+(int)(Math.random()*50000);
		if(userDao.findUserInfoByName(user.getUsername())==null){
			userDao.addUser(user);
			map.put("result", "success");
		}else
			map.put("result", "error");
		return map;
	}
	
	
	public Map<String,String> modifyPassWord(User user){//更改用户密码
		
		Map<String,String> map = new HashMap<String,String>();
		if(userDao.modifyPassword(user)){
			System.out.println("success modifypassword");
			map.put("result", "success");
		}else
			System.out.println("fail modifypassword");
			map.put("result", "error");
		return map;
	}
	
	public Map<String,String> modifyUserInfo(User user){//更改用户信息
		Map<String,String> map = new HashMap<String,String>();
		if(userDao.modifyUserInfo(user.getUsername(),user.getOldpwd(),user.getAddress()
				,user.getPhonenumber(),user.isType(),user.getPayname())){
			System.out.println(user.getUsername()+user.getOldpwd()+user.getAddress()
				+user.getPhonenumber()+user.isType()+user.getPayname());
			map.put("result", "success");
		}else
			map.put("result", "error");
		return map;
	}
}
